import 'package:flutter/material.dart';
import 'package:player_plugin/video_control_bar_style.dart';
import 'package:player_plugin/video_top_bar_style.dart';

class VideoTopBar extends AnimatedWidget {
  VideoTopBar({
    Key? key,
    required this.animation,
    VideoControlBarStyle? videoControlBarStyle,
    VideoTopBarStyle? videoTopBarStyle,
    this.onPop,
  })  : videoTopBarStyle = videoTopBarStyle ?? VideoTopBarStyle(),
        videoControlBarStyle = videoControlBarStyle ?? VideoControlBarStyle(),
        super(key: key, listenable: animation);

  final VideoTopBarStyle videoTopBarStyle;
  final VideoControlBarStyle videoControlBarStyle;
  final VoidCallback? onPop;
  final Animation<double> animation;

  @override
  Widget build(BuildContext context) {
    if (videoTopBarStyle.customBar == null) {
      return Positioned(
        top: animation.value,
        left: 0,
        right: 0,
        child: Container(
          margin: videoTopBarStyle.margin,
          padding: videoTopBarStyle.padding,
          height: videoTopBarStyle.height,
          decoration: BoxDecoration(
              gradient: LinearGradient(
            colors: [
              videoControlBarStyle.barBackgroundColor,
              Colors.transparent
            ],
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
          )),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              /// 返回按钮
              GestureDetector(
                behavior: HitTestBehavior.translucent,
                onTap: onPop,
                child: SizedBox(
                  width: 44.0,
                  child: Align(
                    alignment: Alignment.centerLeft,
                    child: videoTopBarStyle.popIcon,
                  ),
                ),
              ),

              /// 中部控制栏
              Expanded(
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: videoTopBarStyle.contents,
                ),
              ),

              /// 右侧部控制栏
              Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: videoTopBarStyle.actions,
              )
            ],
          ),
        ),
      );
    } else {
      return videoTopBarStyle.customBar!;
    }
  }
}
